import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 * User: xsicrz
 * Date: 5/31/11
 * Time: 11:00 AM
 * To change this template use File | Settings | File Templates.
 */
public class Saisie implements Serializable {
    private Date date;

    private Date dateMatinDebut;
    private Date dateMatinFin;

    private Date dateApresMidiDebut;
    private Date dateApresMidiFin;


    private int tauxActivite = 100;


    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getDateApresMidiDebut() {
        return dateApresMidiDebut;
    }

    public void setDateApresMidiDebut(Date dateApresMidiDebut) {
        this.dateApresMidiDebut = dateApresMidiDebut;
    }

    public Date getDateApresMidiFin() {
        return dateApresMidiFin;
    }

    public void setDateApresMidiFin(Date dateApresMidiFin) {
        this.dateApresMidiFin = dateApresMidiFin;
    }

    public Date getDateMatinDebut() {
        return dateMatinDebut;
    }

    public void setDateMatinDebut(Date dateMatinDebut) {
        this.dateMatinDebut = dateMatinDebut;
    }

    public Date getDateMatinFin() {
        return dateMatinFin;
    }

    public void setDateMatinFin(Date dateMatinFin) {
        this.dateMatinFin = dateMatinFin;
    }

    public int getTauxActivite() {
        return tauxActivite;
    }

    public void setTauxActivite(int tauxActivite) {
        this.tauxActivite = tauxActivite;
    }

    public int getYear() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    public int getMonth() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH);
    }


    public int getDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    private boolean existsSaisie() {
        if(dateMatinDebut != null) {
            return true;
        }
        if(dateMatinFin != null) {
            return true;
        }
        if(dateApresMidiDebut != null) {
            return true;
        }
        if(dateApresMidiFin != null) {
            return true;
        }
        return false;
    }

    public long getTempsTotalEnMillis() {
        if(existsSaisie()) {
            long nbMillisMatin = 0;
            if(dateMatinDebut != null && dateMatinFin != null) {
                nbMillisMatin = dateMatinFin.getTime() - dateMatinDebut.getTime();
            }
            long nbMillisApresMidi = 0;
            if(dateApresMidiDebut != null && dateApresMidiFin != null) {
                nbMillisApresMidi = dateApresMidiFin.getTime() - dateApresMidiDebut.getTime();
            }
            return nbMillisMatin +nbMillisApresMidi;
        }   return 0;
    }


    private static long TEMPS_DUNE_JOURNEE_EN_MS =  29880000;

  //  private static long TEMPS_DUNE_DEMI_JOURNEE_EN_MS =  14940000;

    public long getDifferenceEnMillis() {
        if(existsSaisie()) {
            long tempsTotalEffectueEnMillis = getTempsTotalEnMillis();
            return tempsTotalEffectueEnMillis - (TEMPS_DUNE_JOURNEE_EN_MS * tauxActivite / 100);

        } else {
            return 0;
        }


    }


   public String getTempsTotalFormatee() {
       return humanFormatMilliseconds(getTempsTotalEnMillis());
   }

   public String getDifferenceFormatee() {
       return humanFormatMilliseconds(getDifferenceEnMillis());
   }

   private String humanFormatMilliseconds(long milliseconds){
        if(existsSaisie()) {
            long hours = TimeUnit.MILLISECONDS.toHours(milliseconds);
            long hoursInMillis = hours * 60 * 60 * 1000;
            long remaingMinutesInMillis = milliseconds - hoursInMillis;
            long minutes = TimeUnit.MILLISECONDS.toMinutes(remaingMinutesInMillis);
            if(hours == 0) {
                return minutes+"min";
            }
            if(hours < 0) {
                if(minutes < 10 && minutes > -10) {
                    return hours+"h0"+(-minutes);
                } else {
                    return hours+"h"+(-minutes);
                }
            }
            if(minutes < 10) {
                return hours+"h0"+minutes;
            } else {
                return hours+"h"+minutes;
            }
        }
        return null;
    }
}
